import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

//应用模块路由,eager true 代表开启懒加载
const modules: Record<string, any> = import.meta.glob(['./module/*.ts'], { eager: true }) //vite提供的glob导入模块功能，动态导入路由文件

//基础路由配置
const publicModule = [
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue'),
    meta: {},
    children: []
  }
]

//配置路由表
const routes: Array<RouteRecordRaw> = []
//遍历模块，将对应模块路由添加进路由表
Object.keys(modules).forEach(key => {
  const module = modules[key].default
  routes.push(module)
})

//添加基础路由
routes.push(...publicModule)

//创建路由
const router = createRouter({
  history: createWebHistory(),
  routes
})
//创建路由守卫
router.beforeEach(async (_to, _from, next) => {
  NProgress.start()
  next()
})
router.afterEach((_to) => {
  NProgress.done()
})
export default router